package cn.xfdzx.android.apps.shop.presenter;

import cn.xfdzx.android.apps.shop.base.BasePresenter;
import cn.xfdzx.android.apps.shop.bean.ShopInOrderAfterSalesListBeen;
import cn.xfdzx.android.apps.shop.bean.ShopInPiFaOrderAfterSalesListBeen;
import cn.xfdzx.android.apps.shop.bean.ShopOrderListBeen;
import cn.xfdzx.android.apps.shop.contract.ShopInOrderAfterSalesListContract;
import cn.xfdzx.android.apps.shop.contract.ShopInOrderListContract;
import cn.xfdzx.android.apps.shop.model.ShopInOrderAfterSalesListModel;
import cn.xfdzx.android.apps.shop.model.ShopInOrderListModel;
import cn.xfdzx.android.apps.shop.net.RxScheduler;
import io.reactivex.functions.Consumer;

/**
 * @author azheng
 * @date 2018/6/4.
 * Description：
 */
public class ShopInOrderAfterSalesListPresenter extends BasePresenter<ShopInOrderAfterSalesListContract.View> implements ShopInOrderAfterSalesListContract.Presenter {

    private ShopInOrderAfterSalesListContract.Model model;

    public ShopInOrderAfterSalesListPresenter() {
        model = new ShopInOrderAfterSalesListModel();
    }



    @Override
    public void initData(boolean isRefresh, boolean isLoading, String storeId, String salesMode, String status, String keyWord, String page, String limit) {
        if (!isViewAttached()) {
            return;
        }

        if (isLoading) {
            mView.showLoading();
        }
        model.initData(storeId,salesMode,status,keyWord, page, limit)
                .compose(RxScheduler.<ShopInOrderAfterSalesListBeen>Flo_io_main())
                .as(mView.<ShopInOrderAfterSalesListBeen>bindAutoDispose())
                .subscribe(new Consumer<ShopInOrderAfterSalesListBeen>() {
                    @Override
                    public void accept(ShopInOrderAfterSalesListBeen bean) throws Exception {
                        mView.onShopInOrderAfterSalesListSuccess(bean);
                        if (isLoading) {

                            mView.hideLoading();}

                        if (isRefresh) {
                            mView.finishRefresh();
                        } else {
                            mView.finishLoadMore();
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        if (isLoading) {
                            mView.hideLoading();}
                        if (isRefresh) {
                            mView.finishRefresh();
                        } else {
                            mView.finishLoadMore();
                        }
                    }
                });
    }

    @Override
    public void initPifaData(boolean isRefresh, boolean isLoading, String storeId, String keyWord, String page, String limit) {
        if (!isViewAttached()) {
            return;
        }

        if (isLoading) {
            mView.showLoading();
        }
        model.initPifaData(storeId, keyWord, page, limit)
                .compose(RxScheduler.<ShopInPiFaOrderAfterSalesListBeen>Flo_io_main())
                .as(mView.<ShopInPiFaOrderAfterSalesListBeen>bindAutoDispose())
                .subscribe(new Consumer<ShopInPiFaOrderAfterSalesListBeen>() {
                    @Override
                    public void accept(ShopInPiFaOrderAfterSalesListBeen bean) throws Exception {
                        mView.onPifaShopInOrderAfterSalesListSuccess(bean);
                        if (isLoading) {

                            mView.hideLoading();}

                        if (isRefresh) {
                            mView.finishRefresh();
                        } else {
                            mView.finishLoadMore();
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        if (isLoading) {
                            mView.hideLoading();}
                        if (isRefresh) {
                            mView.finishRefresh();
                        } else {
                            mView.finishLoadMore();
                        }
                    }
                });
    }
}
